package charlie.com.courier;

import charlie.shop.misc.Common;
import com.charlie.beans.ShipmentOrder;
import com.charlie.sessions.ShipmentOrderFacadeLocal;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Display couriers orders servlet
 * 
 * @Alexandra
 */
@WebServlet(name = "OrdersForCourier", urlPatterns = {"/courier/orders"})
public class OrdersForCourier extends HttpServlet {

    @EJB
    private ShipmentOrderFacadeLocal shipmentOrder;
    /**
     * Items per page limit static constant
     */
    private static final int ITEMS_PER_PAGE = 20;
    private static final Integer SHIPPING_STATUS = 2;


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        int page = Common.getPage(request);
        int itemsCount = shipmentOrder.countByID(SHIPPING_STATUS);
        int pagesCount = (int) Math.ceil((float) itemsCount / (float) ITEMS_PER_PAGE);
        List<ShipmentOrder> records = null;
       
        if (page <= pagesCount) {
             records = shipmentOrder.findByStatus(SHIPPING_STATUS,new Integer (((page - 1) * ITEMS_PER_PAGE)), new Integer((page * ITEMS_PER_PAGE)));
        }
        
        request.setAttribute("orderList",records);
      
        request.setAttribute("page", page);
        request.setAttribute("pagesCount", pagesCount);
        request.getRequestDispatcher("/courier/listOfOrdersCourier.jsp").forward(request, response);
    }
}
